import {createApp, inject, provide, ref} from 'vue'
import './assets/styles/index.scss'
import {
  ElButton,
  ElContainer,
  ElAside,
  ElMenu,
  ElSubMenu,
  ElMenuItemGroup,
  ElMenuItem,
  ElForm,
  ElFormItem,
  ElInput,
  ElCheckbox,
  ElTag,
  ElCard,
  ElTable,
  ElTableColumn,
  ElDialog,
  ElRadioGroup,
  ElRadio,
  ElSelect,
  ElMessage,
  ElOption
} from 'element-plus'
import App from './App.vue'
import router from '@/router'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import registerI18n from './language/register-i18n'
import {setToken} from "@/utils/auth";
import x2js from 'x2js'
import {localGet} from "@/utils";

const app = createApp(App) // 生成 Vue 实例 app
app.directive('disabled', {
  mounted(el, binding, vnode, prevVnode) {
    const loginInfo = localGet('token')
    console.log(el,binding.instance)
    for (let i = 0; i < el.length; i++) {
      const element = el[i]
      element.setAttribute('disabled','')
      element.style.cursor = 'not-allowed'
      element.style.backgroundColor = '#fbfbfb'
    }
  }
})
import permission from "@/utils/permission";
app.directive('permissionShow', {
  mounted(el, binding, vnode, prevVnode) {
    const loginInfo = localGet('token')
    // console.log('AASDFASDF',el,binding.value,loginInfo)
    // 如果没有传bindingvalue默认为只读被隐藏，显示2,99
    if (!binding.value) {
      binding.value = permission.adminAndWrite
      // console.log('指令没传值,赋值2,99',binding.value)
    }
    if (!binding.value.includes(loginInfo?.level)) {
      // console.log('所需权限列表不包含当前权限，隐藏元素', loginInfo.level,binding.value)
      el.style.display = 'none'
    }
  }
})

const $x2js = new x2js()
app.provide('x2js', $x2js)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// 全局方法
app.config.globalProperties.$filters = {

}

app.config.globalProperties.goTop = function () {
  const main = document.querySelector('.main')
  main.scrollTop = 0
}

setToken('c6cdc63d0b96f199c91ee64d393af8048d759c93')

app.use(router) // 引用路由实例
app.use(registerI18n)
app.use(ElButton)
  .use(ElContainer)
  .use(ElAside)
  .use(ElMenu)
  .use(ElSubMenu)
  .use(ElMenuItemGroup)
  .use(ElMenuItem)
  .use(ElForm)
  .use(ElFormItem)
  .use(ElCheckbox)
  .use(ElInput)
  .use(ElTag)
  .use(ElCard)
  .use(ElTable)
  .use(ElTableColumn)
  .use(ElDialog)
  .use(ElRadioGroup)
  .use(ElRadio)
  .use(ElSelect)
  .use(ElOption)
  .use(ElMessage)
app.mount('#app') // 挂载到 #app
